﻿Public Class frmTabHorizontalCoordinate

    Dim editnode As EditXML = New EditXML()
    Public Shared cPlanar As New Collection
    Private Shared sPlanProjParams(29) As String

    Private Sub btnAddPlanar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPlanar.Click
        Dim s1 As String = ""
        Dim oTempPlanar As New PlanarClass
        Dim frmNewPlanar As New frmPlanar
        frmNewPlanar.ShowDialog()
        If (frmNewPlanar.getUpdated() = True) Then
            oTempPlanar = frmNewPlanar.getPlanar()
            cPlanar.Add(oTempPlanar)
            If (oTempPlanar.MapProj = True) Then
                s1 = "Map: " & oTempPlanar.MapProjName
            ElseIf (oTempPlanar.GridSys = True) Then
                s1 = "Grid: " & oTempPlanar.GridName
                If (oTempPlanar.GridName <> "other grid system") Then
                    s1 += ", Projection: " & oTempPlanar.GridProjName
                End If
            Else
                s1 = "Local Planar: " & Microsoft.VisualBasic.Left(oTempPlanar.LocalDesc, 40)
            End If
            lboxPlanar.Items.Add(s1)
        End If
    End Sub

    Private Sub btnEditPlanar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditPlanar.Click
        Dim sError As String = ""
        Dim iCount As Integer = lboxPlanar.Items.Count
        Dim iSelInd As Integer = lboxPlanar.SelectedIndex 'get selected planar definition to edit
        If (iCount = 1) Then iSelInd = 0 'if only 1 entry, default to edit it
        If (iSelInd = -1) Then
            sError = "No Planar Definition selected for editing!"
            MessageBox.Show(sError, "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim s1 As String = ""
        Dim oTempPlanar As New PlanarClass
        oTempPlanar = CType(cPlanar(iSelInd + 1), PlanarClass)
        Dim frmEditPlanar As New frmPlanar(oTempPlanar)
        frmEditPlanar.ShowDialog()
        If (frmEditPlanar.getUpdated() = True) Then
            cPlanar.Add(frmEditPlanar.getPlanar(), , iSelInd + 1) 'insert updated Planar Def
            cPlanar.Remove(iSelInd + 2) 'remove obsolete Planar Def
            oTempPlanar = frmEditPlanar.getPlanar()
            If (oTempPlanar.MapProj = True) Then
                s1 = "Map: " & oTempPlanar.MapProjName
            ElseIf (oTempPlanar.GridSys = True) Then
                s1 = "Grid: " & oTempPlanar.GridName
                If (oTempPlanar.GridName <> "other grid system") Then
                    s1 += ", Projection: " & oTempPlanar.GridProjName
                End If
            Else
                s1 = "Local Planar: " & Microsoft.VisualBasic.Left(oTempPlanar.LocalDesc, 40)
            End If
            lboxPlanar.Items.Insert(iSelInd, s1) 'insert updated Planar Def
            lboxPlanar.Items.RemoveAt(iSelInd + 1) 'remove obsolete Planar Def
        End If
        btnEditPlanar.Enabled = False
        btnDelPlanar.Enabled = False
    End Sub

    Private Sub btnDelPlanar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelPlanar.Click
        Dim sError As String = ""
        Dim iSelIndex As Integer = lboxPlanar.SelectedIndex
        If (iSelIndex = -1) Then
            sError = "No Planar Definition selected for deletion!"
            MessageBox.Show(sError, "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        cPlanar.Remove(iSelIndex + 1) '1-based index
        lboxPlanar.Items.RemoveAt(iSelIndex) '0-based index
        btnEditPlanar.Enabled = False
        btnDelPlanar.Enabled = False
    End Sub

    Private Sub lboxPlanar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxPlanar.SelectedIndexChanged
        If lboxPlanar.SelectedItems.Count() > 0 Then btnEditPlanar.Enabled = True : btnDelPlanar.Enabled = True Else btnEditPlanar.Enabled = False : btnDelPlanar.Enabled = False
    End Sub

    Private Sub rdoGeographic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoGeographic.CheckedChanged
        pnlGeographic.Enabled = True
        pnlPlanar.Enabled = False
        pnlLocal.Enabled = False
    End Sub

    Private Sub rdoPlanar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoPlanar.CheckedChanged
        pnlGeographic.Enabled = False
        pnlPlanar.Enabled = True
        pnlLocal.Enabled = False
    End Sub

    Private Sub rdoLocal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoLocal.CheckedChanged
        pnlGeographic.Enabled = False
        pnlPlanar.Enabled = False
        pnlLocal.Enabled = True
    End Sub

    Private Sub rdoNone_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoNone.CheckedChanged
        pnlGeographic.Enabled = False
        pnlPlanar.Enabled = False
        pnlLocal.Enabled = False
    End Sub

    Private Sub frmTabHorizontalCoordinate_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        editnode.Edit("/metadata/spref/horizsys/geodetic/horizdn", cboxHorizName.SelectedItem)
        editnode.Edit("/metadata/spref/horizsys/geodetic/ellips", cboxEllipsoid.SelectedItem)
        editnode.Edit("/metadata/spref/horizsys/geodetic/semiaxis", numSemiMajor.Value.ToString())
        editnode.Edit("/metadata/spref/horizsys/geodetic/denflat", numFlatten.Value.ToString())

        editnode.Remove("/metadata/spref/horizsys", "geograph")
        editnode.Remove("/metadata/spref/horizsys", "planar")
        editnode.Remove("/metadata/spref/horizsys", "local")

        Dim newNode As String = ""
        If rdoGeographic.Checked Then
            newNode = "<latres>" & numLatRes.Value.ToString() & "</latres><longres>" & numLongRes.Value.ToString() & "</longres><geogunit>" & cboxGeoCoorUnits.SelectedItem & "</geogunit>"
            editnode.Add("/metadata/spref/horizsys", "geograph", "geograph", newNode)
        ElseIf rdoPlanar.Checked Then
            Dim parameterNames() As String = {"stdparll", "stdparll", "longcm", "latprjo", "feast", "fnorth", "sfequat", "heightpt", "longpc", "latprjc", "sfctrlin", "azimangl", "azimptl", "obqllat", "obqllong", "obqllat", "obqllong", "svlong", "sfprjorg", "landsat", "pathnum", "sfctrmer", "utmzone", "upszone", "spcszone", "arczone", "otherprj"}
            Dim sectionFullNames() As String = {"Albers Conical Equal Area", "Azimuthal Equidistant", "Equidistant Conic", "Equirectangular", "General Vertical Near-sided Projection", "Gnomonic", "Lambert Azimuthal Equal Area", "Lambert Conformal Conic", "Mercator", "Modified Stereographic for Alaska", "Miller Cylindrical", "Oblique Mercator", "Orthographic", "Polar Stereographic", "Polyconic", "Robinson", "Sinusoidal", "Space Oblique Mercator", "Stereographic", "Transverse Mercator", "van der Grinten"}
            Dim sectionNames() As String = {"albers", "azimequi", "equicon", "equirect", "gvnsp", "gnomonic", "lamberta", "lambertc", "mercator", "modsak", "miller", "obqmerc", "orthogr", "polarst", "polycon", "robinson", "sinusoid", "spaceobq", "stereo", "transmer", "vdgrin"}
            Dim sectionFullNames2() As String = {"Universal Transverse Mercator", "Universal Polar Stereographic", "State Plane Coordinate System 1927", "State Plane Coordinate System 1983", "ARC Coordinate System", "other grid system"}
            Dim sectionNames2() As String = {"utm", "ups", "spcs", "spcs", "arcsys", "othergrd"}

            For i As Integer = cPlanar.Count() To 1 Step -1
                Dim planarItem As PlanarClass = cPlanar(i)
                'Map Projection
                If planarItem.MapProj Then
                    newNode = "<mapproj><mapprojn>" & planarItem.MapProjName & "</mapprojn>"
                    Dim sectionID As Integer = -1
                    For k As Integer = 1 To sectionFullNames.GetLength(0)
                        If sectionFullNames(k - 1) = planarItem.MapProjName Then sectionID = k - 1
                    Next
                    If sectionID <> -1 Then newNode = newNode & "<" & sectionNames(sectionID) & ">"
                    For j As Integer = 3 To planarItem.nParams()
                        Dim parameter As String = planarItem.getParam(j - 1)
                        If parameter <> "" And j = 14 Then newNode = newNode & "<obqlazim>"
                        If parameter <> "" And j = 16 Then newNode = newNode & "<obqlpt>"
                        If parameter <> "" Then newNode = newNode & "<" & parameterNames(j - 3) & ">" & parameter & "</" & parameterNames(j - 3) & ">"
                        If parameter <> "" And j = 15 Then newNode = newNode & "</obqlazim>"
                        If parameter <> "" And j = 19 Then newNode = newNode & "</obqlpt>"
                    Next
                    If sectionID <> -1 Then newNode = newNode & "</" & sectionNames(sectionID) & ">"
                    newNode = newNode & planci(planarItem)
                    newNode = newNode & "</mapproj>"
                    'Grid Coordinate System
                ElseIf planarItem.GridSys Then
                    newNode = newNode & "<gridsys><gridsysn>" & planarItem.GridName & "</gridsysn>"
                    Dim sectionID As Integer = -1
                    Dim sectionID2 As Integer = -1
                    For k As Integer = 1 To sectionFullNames2.GetLength(0)
                        If sectionFullNames2(k - 1) = planarItem.GridName Then sectionID = k - 1
                    Next
                    If sectionID <> -1 Then newNode = newNode & "<" & sectionNames2(sectionID) & ">"
                    For j As Integer = 3 To planarItem.nParams()
                        Dim parameter As String = planarItem.getParam(j - 1)
                        If j = 3 Then
                            For k As Integer = 1 To sectionFullNames.GetLength(0)
                                If sectionFullNames(k - 1) = planarItem.GridProjName Then sectionID2 = k - 1
                            Next
                            If sectionID2 <> -1 Then newNode = newNode & "<" & sectionNames(sectionID2) & ">"
                        End If
                        If parameter <> "" And j = 14 Then newNode = newNode & "<obqlazim>"
                        If parameter <> "" And j = 16 Then newNode = newNode & "<obqlpt>"
                        If parameter <> "" And j < 25 Then newNode = newNode & "<" & parameterNames(j - 3) & ">" & parameter & "</" & parameterNames(j - 3) & ">"
                        If parameter <> "" And j = 15 Then newNode = newNode & "</obqlazim>"
                        If parameter <> "" And j = 19 Then newNode = newNode & "</obqlpt>"
                        If j = 24 Then
                            If sectionID2 <> -1 Then newNode = newNode & "</" & sectionNames(sectionID2) & ">"
                        End If
                        If parameter <> "" And j >= 25 And j <> 29 Then newNode = newNode & "<" & parameterNames(j - 3) & ">" & parameter & "</" & parameterNames(j - 3) & ">"
                        If parameter <> "" And j = 29 Then newNode = newNode & parameter
                    Next
                    If sectionID <> -1 Then newNode = newNode & "</" & sectionNames2(sectionID) & ">"
                    newNode = newNode & planci(planarItem)
                    newNode = newNode & "</gridsys>"
                    'Local Planar
                ElseIf planarItem.LocalPlanar Then
                    newNode = "<planar><localp>"
                    newNode = newNode & "<localpd>" & planarItem.LocalDesc & "</localpd><localpgi>" & planarItem.LocalGeoRef & "</localpgi>"
                    newNode = newNode & "</localp>"
                    newNode = newNode & planci(planarItem)
                    newNode = newNode & "</planar>"
                End If
                editnode.Add("/metadata/spref/horizsys", "planar", "planar", newNode)
            Next
        ElseIf rdoLocal.Checked Then
            newNode = "<localdes>" & txtLocalDesc.Text.Trim() & "</localdes><localgeo>" & txtLocalGeoInfo.Text.Trim() & "</localgeo>"
            editnode.Add("/metadata/spref/horizsys", "local", "local", newNode)
        End If
    End Sub
    Private Function planci(ByVal item As PlanarClass) As String
        'Planar Coordinate Information
        Dim planciNode As String = ""
        planciNode = "<planci>"
        planciNode = planciNode & "<plance>" & item.Encoding & "</plance>"
        planciNode = planciNode & "<plandu>" & item.PlanarUnits & "</plandu>"
        If item.Encoding = "coordinate pair" Or item.Encoding = "row and column" Then
            planciNode = planciNode & "<coordrep><absres>" & item.Abscissa.ToString() & "</absres><ordres>" & item.Ordinate.ToString() & "</ordres></coordrep>"
        ElseIf item.Encoding = "distance and bearing" Then
            planciNode = planciNode & "<distbrep><distres>" & item.DistanceRes.ToString() & "</distres><bearres>" & item.BearingRes.ToString() & "</bearres><bearunit>" & item.BearingUnits & "</bearunit><bearrefd>" & item.BearingDir & "</bearrefd><bearrefm>" & item.BearingMerid & "</bearrefm></distbrep>"
        End If
        planciNode = planciNode & "</planci>"
        Return planciNode
    End Function

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("HorizontalCoordinate")
        helpForm.ShowDialog()
    End Sub
End Class